﻿<?php
/*
 * 参与活动
 * 1、得到UID
 * 2、报名活动
 * 3、因为来源以判断是否报名
 */
require_once '../init.php';
define('TEMPLATE_PATH', WANYA_ROOT . '/m/view/');

/*$appid = "wxbee55928ce2bfa25";
$secret = "1370b77e269a256ede75d700c8054ce6";

$code = $_GET["code"];

// 得到token
$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
$res=file_get_contents($get_token_url);

$token_obj = json_decode($res,true);
$openid = $token_obj['openid'];
$access_token = $token_obj['access_token'];*/

// TODO 通过openid，去检索用户

$User_Info_Model = new User_Info_Model();

$openid = $_POST['openid'];
$userSimpleInfos = array_filter($User_Info_Model->getViewUserInfo($openid));

//找出活动的属性信息
$Activity_Model = new Activity_Model();
$ActivityResult = array_filter($Activity_Model->selectActivity());
//算出当前活动ID
$activity = $ActivityResult[0];
$activity_id = $_POST['activity_id'];
echo($activity_id);

$userInfo = $userSimpleInfos[0];
$uid = $_POST['uid'];
echo($uid);


if($activity_id==null){
    include View::getView('activity/applySuccess');
    View::output();
}
if($uid==null){
    echo("数据异常");
    include View::getView('activity/applySuccess');
    View::output();
}
//todo 如果没有报名 就显示活动详情的页面
$UserApplyResult = array_filter($Activity_Model->checkUserApply($uid,$activity_id));
// todo 判断是否报名

if(!empty($UserApplyResult)){
    //报名失败！
    $site_title = "报名失败";
    $error = "报名失败";
    //开始跳转
    include View::getView('activity/applyError');
    View::output();

}else{
    $result = $Activity_Model->applyActivity($uid,$activity_id);
    if($result!=null){
        //报名成功！
        $site_title = "报名成功";
        //开始跳转
        include View::getView('activity/applySuccess');
        View::output();
    }else{
        include View::getView('activity/applyError');
        $error = "数据异常";
        View::output();
    }

}



?>